﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Infrastucture;
using Infrastucture.Collections;
using TimeSplicerModule.DomainModels;
using Infrastucture.View;

namespace DashboardModule.ViewModels
{
    public class TempReportsViewModel : ViewModel
    {
        // TODO: Need to add filterd projects for when user is changed
        private Guid _selectedUserGuid;
        private Guid _selectedProjectGuid;
        private UserModel _selectedUser;
        private ProjectModel _selectedProject;
        private List<UserModel> _users;
        private List<ProjectModel> _projects;
        private ObservableCollection<ProjectModel> _filteredProjects;
        private List<ProjectUserModel> _projectUsers;
        private DateTime _startDate;
        private DateTime _endDate;
        private daterange _range; // Try changing this to the class of date range



        public Guid SelectedUserGuid
        {
            get { return _selectedUserGuid; }
            set 
            { 
                SetProperty(ref _selectedUserGuid, value);

                if (this.Users != null && this.Users.Count > 0)
                {
                    this.SelectedUser = this.Users.FirstOrDefault(x => x.UserGuid == this.SelectedUserGuid);

                    if (this.Projects != null && this.ProjectUsers != null)
                    {
                        if (this.FilteredProjects == null)
                            this.FilteredProjects = new ObservableCollection<ProjectModel>();
                        else
                            this.FilteredProjects.Clear();

                        var projIds = this.ProjectUsers.Where(x => x.UserGuid == this.SelectedUserGuid).Select(x => x.ProjectGuid).Distinct().ToList();

                        foreach (var i in projIds)
                        {
                            var p = this.Projects.FirstOrDefault(x => x.ProjectGuid == i);
                            this.FilteredProjects.Add(p);
                        }

                        if (this.FilteredProjects.Count > 0)
                            this.SelectedProjectGuid = this.FilteredProjects.FirstOrDefault().ProjectGuid;
                    }
                }
            }
        }
        public Guid SelectedProjectGuid
        {
            get { return _selectedProjectGuid; }
            set 
            { 
                SetProperty(ref _selectedProjectGuid, value);

                if (this.Projects != null && this.Projects.Count > 0)
                    this.SelectedProject = this.Projects.FirstOrDefault(x => x.ProjectGuid == this.SelectedProjectGuid);
            }
        }
        public UserModel SelectedUser
        {
            get { return _selectedUser; }
            set { SetProperty(ref _selectedUser, value); }
        }
        public ProjectModel SelectedProject
        {
            get { return _selectedProject; }
            set { SetProperty(ref _selectedProject, value); }
        }
        public List<UserModel> Users
        {
            get { return _users; }
            set { SetProperty(ref _users, value); }
        }
        public List<ProjectModel> Projects
        {
            get { return _projects; }
            set { SetProperty(ref _projects, value); }
        }
        public ObservableCollection<ProjectModel> FilteredProjects
        {
            get { return _filteredProjects; }
            set { SetProperty(ref _filteredProjects, value); }
        }
        public List<ProjectUserModel> ProjectUsers
        {
            get { return _projectUsers; }
            set { SetProperty(ref _projectUsers, value); }
        }
        public DateTime StartDate
        {
            get { return _startDate; }
            set 
            { 
                SetProperty(ref _startDate, value);
            }
        }
        public DateTime EndDate
        {
            get { return _endDate; }
            set 
            { 
                SetProperty(ref _endDate, value);
                //Range.EndDate = this.EndDate;
            }
        }
        public daterange Range
        {
            get { return _range; }
            set { SetProperty(ref _range, value); }
        }
    }
}
